package com.fzg.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzg.common.demo.country.ProvinceCityAndCounty;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.entity.ChinaProvinceCity;
import com.fzg.mapper.ChinaProvinceCityMapper;
import com.fzg.service.ChinaProvinceCityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-11-21
 */
@Slf4j
@Service
public class ChinaProvinceCityServiceImpl extends ServiceImpl<ChinaProvinceCityMapper, ChinaProvinceCity> implements ChinaProvinceCityService {

    /**
     * 将全国所有省份，市区，县数据导入到MySQL数据库中
     *
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper importProvinceCityCounty() {

        // 开始计时
        Timestamp startTime = new Timestamp(System.currentTimeMillis());

        // 调用这个类的静态方法把文本内容处理成 数据list
        ArrayList<HashMap<String, Object>> list = ProvinceCityAndCounty.getChineseAllProvincesAndCityCounty("E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\country\\provinceCityAndCounty.txt");
        String res;
        if (list != null){
            int count = 0;
            for (HashMap<String, Object> map : list) {
                boolean save = this.save(new ChinaProvinceCity() {{
                    setChineseName(map.get("chineseName").toString());
                    setEnglishName(map.get("englishName").toString());
                    setCode(map.get("code").toString());
                    setParentCode(map.get("parentCode").toString());
                }});
                if (save){
                    count++;
                }
            }
            res = "新增了" + count + "数据";
        }else {
            res = "数据为空";
        }
        Timestamp endTime = new Timestamp(System.currentTimeMillis());
        // 用时
        long second = (endTime.getTime() - startTime.getTime()) / 1000;
        String result = "用时" + second + "秒； ";
        return ResponseWrapper.markCustomSuccess(result + res);
    }

    /**
     * 获取树状结构的省市区数据
     *
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getTreeProvinceCityCountyData() {
        Timestamp startTime = new Timestamp(System.currentTimeMillis());
        ArrayList<ChinaProvinceCity> list = this.baseMapper.getAllSort();
        Timestamp endTime = new Timestamp(System.currentTimeMillis());
        // 用时
        long second = (endTime.getTime() - startTime.getTime()) / 1000;
        log.info("获取树形结构数据用时" + second + "秒");
        return ResponseWrapper.markSuccess(list);
    }
}
